import 'dart:convert';

import 'package:ride_rex/constant.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../article/post/article_post_bean.dart';

// class LocalDataUtils {
//   static final LocalDataUtils _instance = LocalDataUtils._internal();
//
//   factory LocalDataUtils() => _instance;
//
//   LocalDataUtils._internal();
//
//   final String _key = '${CommonStorage().getString(StorageKey.spKeyUserId)}_local_data';
//
//   ///拉黑
//   static Future block(BlockListBean blockListBean, {bool unblock = false}) async {
//     String cache = CommonStorage().getString(StorageKey.spKeyBlockList);
//     List<BlockListBean> beanList = [];
//     try {
//       List list = json.decode(cache);
//       beanList = list.map((e) => BlockListBean.fromJson(e)).toList();
//     } catch (e) {
//       beanList = [];
//     }
//
//     int index = beanList.indexWhere((element) => element.userId == blockListBean.userId);
//     if (unblock) {
//       ///解除拉黑
//       if (index >= 0) beanList.removeAt(index);
//     } else {
//       ///拉黑
//       blockListBean.updateTime = DateTime.now().toString();
//       if (index >= 0) beanList.removeAt(index);
//       beanList.add(blockListBean);
//     }
//
//     await CommonStorage().setString(StorageKey.spKeyBlockList, json.encode(beanList));
//   }
// }

class MyPostArticleUtils {
  static final MyPostArticleUtils _instance = MyPostArticleUtils._internal();

  factory MyPostArticleUtils() => _instance;

  MyPostArticleUtils._internal();

  final String _key = '${CommonStorage().getString(StorageKey.spKeyUserId)}_my_post_article';

  ///保存
  static Future add(PostArticleBean bean) async {
    String cache = CommonStorage().getString(_instance._key);
    List<PostArticleBean> beanList = [];
    try {
      List list = json.decode(cache);
      beanList = list.map((e) => PostArticleBean.fromJson(e)).toList();
    } catch (e) {
      beanList = [];
    }

    bool isExist = beanList.any((element) => element.recordId == bean.recordId);
    if (!isExist) {
      beanList.add(bean);
      await CommonStorage().setString(_instance._key, json.encode(beanList));
    }
  }

  ///清空
  static Future clear() async => await CommonStorage().remove(_instance._key);

  ///删除
  static Future remove(String recordId) async {
    String cache = CommonStorage().getString(_instance._key);
    List<PostArticleBean> beanList = [];
    try {
      List list = json.decode(cache);
      beanList = list.map((e) => PostArticleBean.fromJson(e)).toList();
    } catch (e) {
      beanList = [];
    }

    beanList.removeWhere((element) => element.recordId == recordId);
    await CommonStorage().setString(_instance._key, json.encode(beanList));
  }

  ///获取
  static List<PostArticleBean> get() {
    String cache = CommonStorage().getString(_instance._key);
    List<PostArticleBean> beanList = [];
    try {
      List list = json.decode(cache);
      beanList = list.map((e) => PostArticleBean.fromJson(e)).toList();
    } catch (e) {
      beanList = [];
    }
    beanList.sort(
      (a, b) {
        int time1 = int.tryParse(a.createTime ?? '') ?? 0;
        int time2 = int.tryParse(b.createTime ?? '') ?? 0;
        return time2.compareTo(time1);
      },
    );
    return beanList;
  }
}
